package com.hedl.practice.diyJava.io.practice;

import java.io.*;

/**
 * @author Big Watermelon
 * @description 复制拷贝的io写法
 * @date 2024/02/14/17:09
 */
public class Copy {

    private final static String SOURCEPATH = "/Users/a101/hello.txt";
    private final static String DESTINATIONPATH = "/Users/a101/es.txt";

    private final static String str = "你好，大西瓜！";

    public static void testInputAndOutput(){
        try (InputStream inputStream = new FileInputStream(SOURCEPATH);
             OutputStream outputStream = new FileOutputStream(DESTINATIONPATH)){
            byte[] buf = new byte[1024];
            int len;
            while ((len = inputStream.read(buf)) != -1){
                outputStream.write(buf,0,len);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void testBufferWriter(String str,String address){
        try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(address))){
            bufferedWriter.write(str);
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void testBufferReader(String address){
        try(BufferedReader bufferedReader = new BufferedReader(new FileReader(address))){
            String str;
            while ((str = bufferedReader.readLine()) != null){
                System.out.println(str);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void testAppend(String str,boolean bool,String address){
        if (bool){
            try(BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(address,bool))){
                bufferedWriter.write(str);
            }catch (IOException e){
                e.printStackTrace();
            }
        }else {
            try(BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(address))){
                bufferedWriter.write(str);
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        //testBufferWriter(str);
        //testInputAndOutput();
        //testBufferReader();

        testAppend("你好，大西瓜！\n",false,SOURCEPATH);
        testBufferReader(SOURCEPATH);

    }

}
